﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import Engine.Managers.*;
    import flash.utils.*;
    import Display.DialogUI.*;
    import Classes.util.*;
    import Display.*;
    import Modules.stats.types.*;

    public class ConnectionStatus {

        public static const TIMEOUT_SAVE_GAME:String = "timeout_save_game";
        public static const TIMEOUT_CONNECTION_LOST:String = "timeout_connection_lost";

        protected static var m_currentTimeoutType:String;
        protected static var m_timeoutTimer:Timer;
        protected static var m_timeoutDialog:GenericDialog;
        protected static var m_onTimeout:Function;
        private static var m_zaspLoadHandle:int = 0;

        public static function startTimeout(_arg1:String, _arg2:int, _arg3:Boolean=true, _arg4:Function=null):void{
            var _local5:String;
            if (m_currentTimeoutType != null){
                return;
            };
            m_currentTimeoutType = _arg1;
            m_timeoutTimer = TimerUtil.callLater(onTimeout, _arg2);
            m_onTimeout = _arg4;
            if (_arg3){
                StatsManager.count(StatsCounterType.ERRORS, (StatsKingdomType.TRANSACTIONS + "_start"), m_currentTimeoutType);
                switch (m_currentTimeoutType){
                    case TIMEOUT_SAVE_GAME:
                        _local5 = ZLoc.t("Main", "SavingFarm");
                        break;
                    case TIMEOUT_CONNECTION_LOST:
                        _local5 = ZLoc.t("Main", "RetryingConnection");
                        break;
                    default:
                        _local5 = ZLoc.t("Main", "SavingFarm");
                };
                m_timeoutDialog = UI.displayMessage(_local5, GenericDialogView.TYPE_NOBUTTONS, null, "", true);
                if (GlobalEngine.zaspManager){
                    m_zaspLoadHandle = GlobalEngine.zaspManager.zaspWaitStart("TRANSACTION_QUEUE", m_currentTimeoutType);
                };
            };
        }
        public static function stopTimeout():void{
            m_currentTimeoutType = null;
            m_onTimeout = null;
            if (m_timeoutDialog != null){
                m_timeoutDialog.close();
                m_timeoutDialog = null;
            };
            if (m_zaspLoadHandle > 0){
                GlobalEngine.zaspManager.zaspWaitEnd(m_zaspLoadHandle);
                m_zaspLoadHandle = 0;
            };
            if (m_timeoutTimer != null){
                m_timeoutTimer.stop();
                m_timeoutTimer = null;
            };
        }
        protected static function onTimeout():void{
            var _local1:String;
            StatsManager.count(StatsCounterType.ERRORS, StatsKingdomType.TRANSACTIONS, m_currentTimeoutType);
            if (m_onTimeout != null){
                m_onTimeout(m_currentTimeoutType);
            };
            switch (m_currentTimeoutType){
                case TIMEOUT_SAVE_GAME:
                    _local1 = ZLoc.t("Main", "SaveGameTimeout");
                    break;
                case TIMEOUT_CONNECTION_LOST:
                    _local1 = ZLoc.t("Main", "ConnectionLostTimeout");
                    break;
                default:
                    _local1 = ZLoc.t("Main", "RefreshGame");
            };
            UI.displayMessage(_local1, GenericPopup.TYPE_OK, GameUtil.redirectHome, "", true);
        }

    }
}//package Classes 
